package com.gi.product.controller;

import com.gi.product.dto.CartDTO;
import com.gi.product.entity.ProductCategory;
import com.gi.product.entity.ProductInfo;
import com.gi.product.service.ProductCategoryService;
import com.gi.product.service.ProductInfoService;
import com.gi.product.utils.ResultVoUtils;
import com.gi.product.vo.ProductInfoVo;
import com.gi.product.vo.ProductVo;
import com.gi.product.vo.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class ProductInfoController {
    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductCategoryService productCategoryService;

    @GetMapping("/findAllProductInfo")
    public List<ProductInfo> findAllProductInfo() {
        List<ProductInfo> productInfoList = productInfoService.findAllProductInfo();
        return productInfoList;
    }

    @GetMapping("/msg")
    public String msg() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "product msg";
    }


    @GetMapping("/findUpProductInfo")
    public ResultVo<ProductVo> findUpProductInfo() {
        //1. 查询所有在架的商品
        List<ProductInfo> productInfoList = productInfoService.findUpProductInfo();

        //2. 获取类目type列表
        List<Integer> categoryTypeList = productInfoList.stream()
                .map(ProductInfo::getCategoryType)
                .collect(Collectors.toList());

        Map<String, Object> map = new HashMap<>();
        map.put("categoryTypes", categoryTypeList);
//        map.put("categoryId",5);
        //3. 从数据库查询类目
        List<ProductCategory> categoryList = productCategoryService.findProductCategoryListByMap(map);
//        List<ProductCategory> categoryList = productCategoryService.findProductCategoryList(categoryTypeList);

        //4. 构造数据
        List<ProductVo> productVOList = new ArrayList<>();
        for (ProductCategory productCategory : categoryList) {
            ProductVo productVO = new ProductVo();
            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());

            List<ProductInfoVo> productInfoVOList = new ArrayList<>();
            for (ProductInfo productInfo : productInfoList) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
                    ProductInfoVo productInfoVO = new ProductInfoVo();
                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }

        return ResultVoUtils.success(productVOList);
    }

    /**
     * 获取商品列表(给订单服务用的)
     *
     * @param productIdList
     * @return
     */
    @PostMapping("/listForOrder")
    public List<ProductInfo> listForOrder(@RequestBody List<String> productIdList) {
        System.out.println(productIdList.toString());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return productInfoService.findList(productIdList);
    }

    /**
     * 扣库存
     * @param cartDTOList
     */
    @PostMapping("/decreaseStock")
    public void decreaseStock(@RequestBody List<CartDTO> cartDTOList) {
        System.out.println(cartDTOList.toString());
        productInfoService.decreaseStock(cartDTOList);
    }
}
